package com.hmall.service;

import com.hdmall.business.common.common.Const;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.controller.JpushController;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hmall.util.EnvUtil;
import com.hdmall.business.common.util.HttpClientUtil;
import com.hdmall.business.common.util.JsonUtils;
import com.hmall.util.MerchUtil;
import com.hmall.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @ClassName MerchandiserService
 * @Description //
 * @Author zhoutao
 * @Date 2018/10/13 11:23
 * @Version 2.0
 **/
@Service
@Slf4j
public class MerchandiserService {


    private 
    SysUserRoleMapper sysUserRoleMapper;

    private 
    MallOrderRemarkMapper mallOrderRemarkMapper;

    private 
    SysUserDealerMapper sysUserDealerMapper;

    private 
    MallDealerMapper mallDealerMapper;

    private 
    MallOrderMapper mallOrderMapper;

    private 
    SysCustomerMapper sysCustomerMapper;

    private 
    SysDeptCustomerMapper sysDeptCustomerMapper;

    private 
    JpushController jpushController;

    private 
    SysUserMapper userMapper;

    private 
    MallOrderItemMapper mallOrderItemMapper;

    private 
    MallProductMapper mallProductMapper;

    private 
    MallProductForwardStockMapper mallProductForwardStockMapper;

    private 
    MallOrderItemDetailMapper mallOrderItemDetailMapper;

    private 
    SysCustomerCreditMapper sysCustomerCreditMapper;

    private 
    SysCustomerStatementMapper sysCustomerStatementMapper;

    private 
    SysCustomerMapper customerMapper;

    @Lazy
    private 
    TaskService taskService;

    private 
    MallPaymentUnpayMapper mallPaymentUnpayMapper;

    private final Integer NEED_EXAM_ONE = 0, NEED_EXAM_TWO = 3, PASS_EXAM = 1, UNPASS_EXAM = 2;

    private EnvUtil envUtil = new EnvUtil();

    @Autowired
    public MerchandiserService(MallOrderRemarkMapper mallOrderRemarkMapper, SysUserRoleMapper sysUserRoleMapper, MallOrderItemDetailMapper mallOrderItemDetailMapper, MallPaymentUnpayMapper mallPaymentUnpayMapper, SysUserDealerMapper sysUserDealerMapper, MallDealerMapper mallDealerMapper, MallOrderMapper mallOrderMapper, SysCustomerMapper sysCustomerMapper, SysDeptCustomerMapper sysDeptCustomerMapper, SysCustomerMapper customerMapper, JpushController jpushController, MallProductMapper mallProductMapper, MallProductForwardStockMapper mallProductForwardStockMapper, TaskService taskService, SysUserMapper userMapper, SysCustomerStatementMapper sysCustomerStatementMapper, SysCustomerCreditMapper sysCustomerCreditMapper, MallOrderItemMapper mallOrderItemMapper) {
        this.mallOrderRemarkMapper = mallOrderRemarkMapper;
        this.sysUserRoleMapper = sysUserRoleMapper;
        this.mallOrderItemDetailMapper = mallOrderItemDetailMapper;
        this.mallPaymentUnpayMapper = mallPaymentUnpayMapper;
        this.sysUserDealerMapper = sysUserDealerMapper;
        this.mallDealerMapper = mallDealerMapper;
        this.mallOrderMapper = mallOrderMapper;
        this.sysCustomerMapper = sysCustomerMapper;
        this.sysDeptCustomerMapper = sysDeptCustomerMapper;
        this.customerMapper = customerMapper;
        this.jpushController = jpushController;
        this.mallProductMapper = mallProductMapper;
        this.mallProductForwardStockMapper = mallProductForwardStockMapper;
        this.taskService = taskService;
        this.userMapper = userMapper;
        this.sysCustomerStatementMapper = sysCustomerStatementMapper;
        this.sysCustomerCreditMapper = sysCustomerCreditMapper;
        this.mallOrderItemMapper = mallOrderItemMapper;
    }

    /**
     * @Date 2018/11/5 13:03
     * @Param [userId]
     * @return com.hmall.common.ServerResponse<java.util.List<com.hmall.model.MallDealer>>
     * @Description // 得到办事处列表
     */
    public ServerResponse<List<MallDealer>> getDealerList(Integer userId) {

        List<MallDealer> mallDealerList;
        //如果是跟单员列出跟单员绑定的所有的办事处
        if(checkMerch(userId)) {
            List<Integer> sysUserDealerList = sysUserDealerMapper.selectByUserId(userId);
            mallDealerList = mallDealerMapper.selectByDealerIdList(sysUserDealerList);
        }else{
            mallDealerList = mallDealerMapper.selectAll();
        }
        return ServerResponse.createBySuccess(mallDealerList);
    }

    /**
     * @Date 2018/11/5 13:04
     * @Param [userId, dealerId]
     * @return com.hmall.common.ServerResponse<java.util.List<com.hmall.vo.MerchaFilterVo>>
     * @Description // 得到订单筛选项和数目
     */
    public ServerResponse<List<MerchaFilterVo>> getOrderFilter(Integer userId, Integer dealerId) {

        //订单状态为待审核以及待发货的
        Integer deptId = userMapper.selectByPrimaryKey(userId).getDeptId();
        List<Integer> dealerIdList = getDealerIdList(userId, dealerId, deptId);
        //得到用户权限类型
        Integer fStatus = getUserRole(userId);
        //如果是跟单员能看到被自己拒绝的订单但是不能看到被资金拒绝的订单（被送到了业务员待审核3阶段）

        List<MallOrder> mallOrderList = new ArrayList<>();
        if(dealerIdList.size() != 0) {
            mallOrderList = mallOrderMapper.selectByDealerId(fStatus, dealerIdList);
        }
        List<MerchaFilterVo> merchaFilterVoList = new ArrayList<>();
        int needExam = 0, passExam = 0, unPassExam = 0;
        int status = 0;

        for(MallOrder mallOrder : mallOrderList){
            if(checkMerch(userId)){
                status = mallOrder.getStatus2();
            }else if(checkFinance(userId)){
                status = mallOrder.getStatus3();
            }
            //0或者3表示待审核
            //1表示通过审核
            //2表示未通过审核
            if(NEED_EXAM_ONE.equals(status) || NEED_EXAM_TWO.equals(status)) {
                needExam++;
            } else if(PASS_EXAM.equals(status)) {
                passExam++;
            }else if(UNPASS_EXAM.equals(status)) {
                unPassExam++;
            }
            //审核不通过 就不会展示
            //因为前提是业务员通过

        }
        merchaFilterVoList.add(new MerchaFilterVo("待审核", needExam));
        merchaFilterVoList.add(new MerchaFilterVo("审核通过", passExam));
        merchaFilterVoList.add(new MerchaFilterVo("审核驳回", unPassExam));

        return ServerResponse.createBySuccess(merchaFilterVoList);
    }

    /**
     * @Date 2018/11/8 11:28
     * @Param [userId, dealerId, roleStatus, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description // 得到订单列表
     */
    public ServerResponse<PageVo> getOrderList(Integer userId, Integer dealerId, String roleStatus, Integer pageNum, Integer pageSize) {

        List<MerchaOrderVo> merchaOrderVoList = new ArrayList<>();
        Integer deptId = userMapper.selectByPrimaryKey(userId).getDeptId();
        Integer roleId = sysUserRoleMapper.selectRole(userId).getRoleId();
        List<Integer> dealerIdList = getDealerIdList(userId, dealerId, deptId);
        int mStatus = -1;
        String needExamStr = "待审核";
        String passExamStr = "审核通过";
        String unPassExamStr = "审核驳回";
        if(needExamStr.equals(roleStatus)){
            mStatus = 0;
        }else if(passExamStr.equals(roleStatus)){
            mStatus = 1;
        }else if(unPassExamStr.equals(roleStatus)){
            mStatus = 2;
        }

        Integer fStatus = getUserRole(userId);

        //找到在一定条件的数目 分页作用
        //如果是跟单员 deptId = 305 看status2
        int count = mallOrderMapper.selectCountByDealerIdAndRoleStatus(dealerIdList, mStatus, deptId, fStatus);
        int start = pageSize * (pageNum - 1);
        int end = Math.min(count, start + pageSize);

        List<MallOrder> mallOrderList = new ArrayList<>();

        if(dealerIdList.size() != 0) {
            mallOrderList = mallOrderMapper.selectByDealerIdAndRoleStatus(dealerIdList, mStatus, deptId, start, end, fStatus);
        }

        for(MallOrder mallOrder : mallOrderList){
            MerchaOrderVo merchaOrderVo = new MerchaOrderVo();
            Integer customerId = sysDeptCustomerMapper.selectCustomerIdByCustomerUserId(mallOrder.getUserId());
            SysCustomer customer = sysCustomerMapper.selectByPrimaryKey(customerId);
             /*业务员：待审核  待审核（跟单员审核中） 待审核（跟单员驳回） 待审核（资金专员驳回）
              跟单员： 待审核  待审核（资金专员未审核) 待发货
              资金专员:待审核  待发货*/
            merchaOrderVo.setDetialStatus(MerchUtil.getDetailStatus(userId, deptId, roleId, mallOrder));
            merchaOrderVo.setCustomerName(customer.getBusName());
            merchaOrderVo.setPrice(mallOrder.getPayment());
            List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectAllByOrderNo(mallOrder.getOrderNo());
            merchaOrderVo.setMallOrderItemList(mallOrderItemList);

            String status = "";

            Integer tStatus = 0;
            if(Const.MERCH_DEPT_ID.equals(deptId)){
                tStatus = mallOrder.getStatus2();
            }else if(checkFinance(userId)){
                tStatus = mallOrder.getStatus3();
            }
            if(NEED_EXAM_ONE.equals(tStatus) || NEED_EXAM_TWO.equals(tStatus)) {
                status = "待审核";
            } else if(PASS_EXAM.equals(tStatus)) {
                status = "审核通过";
            } else if(UNPASS_EXAM.equals(tStatus)) {
                status = "审核驳回";
            }

            merchaOrderVo.setStatus(status);
            merchaOrderVo.setOrderNo(mallOrder.getOrderNo());
            merchaOrderVo.setRealName(userMapper.selectByPrimaryKey(mallOrder.getUserId()).getRealName());
            merchaOrderVo.setMallDealer(mallDealerMapper.selectByPrimaryKey(mallOrder.getDealerId()));
            merchaOrderVo.setUpdateTime(mallOrder.getUpdateTime());
            merchaOrderVo.setSubmitPerson(mallOrder.getSubmitPerson());
            merchaOrderVo.setPlanCheck(mallOrder.getPlanCheck());
            merchaOrderVo.setSendDate(mallOrder.getSendDate());
            merchaOrderVo.setStatusOne(mallOrder.getStatus1());
            merchaOrderVo.setStatusTwo(mallOrder.getStatus2());
            merchaOrderVo.setStatusThree(mallOrder.getStatus3());

            merchaOrderVoList.add(merchaOrderVo);
        }

        PageVo pageVo = new PageVo(pageNum, pageSize, count, merchaOrderVoList);
        return ServerResponse.createBySuccess(pageVo);

    }


    /**
     * @Date 2018/11/8 10:58
     * @Param [userId, orderNo, jsonOrderItemStr, status, opinion, sendDate, sound]
     * @return com.hmall.common.ServerResponse
     * @Description // 有风险  如果跟单员审核未审核资金未审核 现在跟单员审核通过会更新部分值，目前是前端做了status的判断不会传递进来
     */
    public ServerResponse examOrder(Integer userId, Long orderNo, String jsonOrderItemStr, Integer status, String opinion, String sendDate, String sound) throws InterruptedException {
//                String testStr =
//                "[{\"materialVoList\":[{\"price\":\"12\",\"weight\":\"25\",\"materialCode\":\"DAS01705R11AZ000\"}]," + "\"grade\":\"A\",\"productId\":\"4326\"}]";
//         jsonOrderItemStr = testStr;
//        2018/10/22改

        Map<String ,Class> tMap = new HashMap<>(16);
        tMap.put("materialVoList", MaterialVo.class);
        List<MaterialParentVo> materialParentVoList = JsonUtils.jsonToList(jsonOrderItemStr, MaterialParentVo.class, tMap);

        MallOrder mallOrder = mallOrderMapper.selectAllByOrderNo(orderNo);
        Integer deptId = userMapper.selectByPrimaryKey(userId).getDeptId();

        //判断是否被审核过
        if(Const.MERCH_DEPT_ID.equals(deptId)){
            if(mallOrder.getStatus2() != 0){
                return ServerResponse.createByErrorMessage("跟单员您已经审核过了");
            }
        }

        if(checkFinance(userId)){
            if(mallOrder.getStatus3() != 0){
                return ServerResponse.createByErrorMessage("资金专员您已经审核过了");
            }
        }
        //2018/10/17 修改成审核通过和审核拒绝都要改产品信息
        //审核之后要更改属性
        BigDecimal price = updateOrder(orderNo, mallOrder.getUserId(), materialParentVoList);
        log.info("更新订单信息成功");

        //如果审核通过
        if(Const.PASS_EXAM.equals(status)) {

            //跟单员操作
            if(Const.MERCH_DEPT_ID.equals(deptId)) {
                if (mallOrder.getStatus1() != 1) {
                    return ServerResponse.createByErrorMessage("业务员审核未同意");
                }

                //2018/11/13 去掉资金这一权限 改为到nc系统内部处理
//                if (mallOrder.getStatus3() != 1) {
//                    return ServerResponse.createByErrorMessage("资金专员审核未同意");
//                }
            }

            if(checkFinance(userId)){
                if(mallOrder.getStatus1() != 1){
                    return ServerResponse.createByErrorMessage("业务员审核未同意");
                }
            }

            //如果是跟单员同意订单更新为待发货状态
            if(Const.MERCH_DEPT_ID.equals(deptId)) {

                merchPassExam(orderNo, sendDate, mallOrder.getUserId(), opinion, price);

            }
        }

        Date createTime = new Date();
        createRemark(createTime, userId, orderNo, opinion);
        log.info("创建备注成功");


        //更新跟单员以及资金的状态
        mallOrderMapper.updateRoleStatusWithOrderNo(orderNo, status, deptId);

        return ServerResponse.createBySuccess("审核完成");
    }

    /**
     * @Date 2018/11/19 8:51
     * @Param [orderNo, sendDate, userId, opinion, price]
     * @return void
     * @Description   资金审核通过之后的处理 解耦处理为业务员服务  2018/11/19 改
     */
     void merchPassExam(Long orderNo, String sendDate, Integer userId, String opinion, BigDecimal price) throws InterruptedException {
        mallOrderMapper.updateByOrderNo(20, orderNo);
        log.info("订单啊状态更新为待发货");
        //跟单员同意能修改发货日期
        mallOrderMapper.updateSendDate(orderNo, sendDate);
        log.info("跟单员更新发货日期成功");
        //更新账期和欠款信息
        //更新账期和欠款的节点在这
        Integer customerId = sysDeptCustomerMapper.selectCustomerIdByCustomerUserId(userId);
        SysCustomerCredit sysCustomerCredit = sysCustomerCreditMapper.selectByCustomerId(customerId);
        //如果是现金用户是跟单员审核通过加欠款
        //账期则是发货
        if(null == sysCustomerCredit) {
            updateUnPaymentAndStatement(customerId, price);
        }
        log.info("更新账期和欠款信息成功");
        //三个推送 极光 + PC rabbitmq + 短信
        messagePush(userId, orderNo, opinion);
        log.info("给用户发审核通过信息推送成功");
    }

    /**
     * @Date 2018/11/8 11:27
     * @Param [orderNo, userId, materialParentVoList]
     * @return java.math.BigDecimal
     * @Description // 更新订单状态包括 order order_item order_item_detial 三个表
     */
     BigDecimal updateOrder(Long orderNo, Integer userId,  List<MaterialParentVo> materialParentVoList){

        //删除原来的所有的orderItem
        mallOrderItemMapper.deleteByOrderNo(orderNo);

        BigDecimal price = new BigDecimal(0.0);
        //插入

        for (MaterialParentVo materialParentVo : materialParentVoList) {

            MallOrderItem mallOrderItem = new MallOrderItem();
            mallOrderItem.setUserId(userId);
            mallOrderItem.setOrderNo(orderNo);
            mallOrderItem.setProductId(materialParentVo.getProductId());
            MallProduct mallProduct = mallProductMapper.selectByProductId(materialParentVo.getProductId());
            mallOrderItem.setProductName(mallProduct.getName());
            mallOrderItem.setProductImage(mallProduct.getMainImage());
            mallOrderItem.setCreateTime(new Date());
            mallOrderItem.setGrade(materialParentVo.getGrade());
            mallOrderItem.setCommentCheck(0);
            String []nameStr = mallProduct.getName().split(",", -1);
            mallOrderItem.setProductModel(nameStr[2]);


            //先处理productItemDetial表
            List<MaterialVo> materialVoList = materialParentVo.getMaterialVoList();
            //有物料编码，等级，重量
            //去查库存表

            mallOrderItem.setQuantity(new BigDecimal(0.0));
            mallOrderItem.setCurrentUnitPrice(new BigDecimal(0.0));
            mallOrderItem.setTotalPrice(new BigDecimal(0.0));
            mallOrderItem.setResidualQuantity(new BigDecimal(0.0));
            //先插入 找出itemId
            mallOrderItemMapper.insert(mallOrderItem);

            Integer itemId = mallOrderItemMapper.selectByProductIdAndGradeAndOrderNo(orderNo, materialParentVo.getProductId(), materialParentVo.getGrade());
//                先删除产品详情

            BigDecimal orderItemQuantity = new BigDecimal(0.0);
            BigDecimal orderItemPrice = new BigDecimal(0.0);
            BigDecimal orderItemUnitPrice;
            //需要更新orderItem表的数据...
            //totalPrice quantity residualQuantity
            //代码和业务员审核一直重复了
            for(MaterialVo materialVo : materialVoList){
                //得到物料编码
                String materialCode = materialVo.getMaterialCode();
                MallProductForwardStock mallProductForwardStock = mallProductForwardStockMapper.selectByOneMaterialCode(materialCode);
                MallOrderItemDetail mallOrderItemDetail = new MallOrderItemDetail();
                mallOrderItemDetail.setCurrentUnitPrice(materialVo.getPrice());
                mallOrderItemDetail.setInventoryOrganization(mallProductForwardStock.getInventoryOrganization());
                mallOrderItemDetail.setMaterialCode(materialCode);
                mallOrderItemDetail.setOrderItemId(itemId);
                mallOrderItemDetail.setQuantity(materialVo.getWeight());
                mallOrderItemDetail.setResidualQuantity(materialVo.getWeight());
                mallOrderItemDetail.setTotalPrice(materialVo.getWeight().multiply(materialVo.getPrice()));
                mallOrderItemDetail.setRate(materialVo.getRate());
                mallOrderItemDetail.setExFactoryPrice(materialVo.getExFactoryPrice());
                orderItemPrice = orderItemPrice.add(mallOrderItemDetail.getTotalPrice());
                orderItemQuantity = orderItemQuantity.add(mallOrderItemDetail.getQuantity());
                mallOrderItemDetail.setCreateTime(new Date());
                price = price.add(orderItemPrice);
                mallOrderItemDetailMapper.insert(mallOrderItemDetail);
            }
            orderItemUnitPrice = orderItemPrice.divide(orderItemQuantity);
            mallOrderItemMapper.updatePriceAndTwoQuantity(itemId, orderItemUnitPrice, orderItemPrice, orderItemQuantity);

        }
        mallOrderMapper.updatePaymentWithOrderNo(orderNo, price);

        return price;
    }

    /**
     * @Date 2018/11/8 10:51
     * @Param [createTime, userId, orderNo, opinion]
     * @return void
     * @Description // 创建备注
     */
    private void createRemark(Date createTime, Integer userId, Long orderNo, String opinion){

        //插入备注信息
        MallOrderRemark mallOrderRemark = new MallOrderRemark();
        mallOrderRemark.setCreateTime(createTime);
        mallOrderRemark.setType(getUserRole(userId));
        mallOrderRemark.setOrderNo(orderNo);
        mallOrderRemark.setUserId(userId);
        mallOrderRemark.setContent(opinion);
        //插入备注
        mallOrderRemarkMapper.insert(mallOrderRemark);
    }

    public ServerResponse<MerchaCheckVo> checkPassExam(Integer userId, Long orderNo) {

        Integer deptId = userMapper.selectByPrimaryKey(userId).getDeptId();
            MallOrder mallOrder = mallOrderMapper.selectAllByOrderNo(orderNo);
            List<MerchaStatusVo> merchaStatusVoList = new ArrayList<>();
            merchaStatusVoList.add(new MerchaStatusVo(1, mallOrder.getStatus1()));
            merchaStatusVoList.add(new MerchaStatusVo(2, mallOrder.getStatus2()));
            merchaStatusVoList.add(new MerchaStatusVo(3, mallOrder.getStatus3()));
            Integer checkStatus = 0;

            //跟单员审核前提：业务员通过 财务通过 并且自己未审核
            if(Const.MERCH_DEPT_ID.equals(deptId)) {
                boolean merchExamCheck = mallOrder.getStatus1() == 1 && mallOrder.getStatus3() == 1 &&
                                        (mallOrder.getStatus2() == 0 || mallOrder.getStatus2() == 3);
                if (merchExamCheck) {
                    checkStatus = 1;
                }
                //财务审核前提：业务通过 财务自己未审核
            }else if(checkFinance(userId)){
                boolean financeExamCheck = mallOrder.getStatus1() == 1 && (mallOrder.getStatus3() == 0 || mallOrder.getStatus3() == 3);
                if(financeExamCheck){
                    checkStatus = 1;
                }
            }
            MerchaCheckVo merchaCheckVo = new MerchaCheckVo();
            merchaCheckVo.setCheckStatus(checkStatus);
            merchaCheckVo.setMerchaStatusVoList(merchaStatusVoList);
            return ServerResponse.createBySuccess(merchaCheckVo);
    }

    /**
     * @Date 2018/11/2 16:26
     * @Param [userId, orderNo]
     * @return com.hmall.common.ServerResponse
     * @Description // 跟单员给资金发消息推送
     */
    public ServerResponse remindFinance(Integer userId, Long orderNo) throws IOException, ParseException {

        return jpushController.backPushMessage(userId,8, orderNo.toString());
    }

    private List<Integer> getDealerIdList(Integer userId, Integer dealerId, Integer deptId){

        List<Integer> dealerIdList = new ArrayList<>();
        //如果是跟单员
        if(Const.MERCH_DEPT_ID.equals(deptId)){
            if (null == dealerId) {
                List<MallDealer> mallDealerList = getDealerList(userId).getData();
                for (MallDealer mallDealer : mallDealerList) {
                    dealerIdList.add(mallDealer.getDealerId());
                }
            } else {
                dealerIdList.add(dealerId);
            }
        }else if(checkFinance(userId)){
            dealerIdList = mallDealerMapper.selectAllId();
        }
        return dealerIdList;
    }

    /**
     * @Date 2018/11/2 16:27
     * @Param [userId]
     * @return boolean
     * @Description // 判断是否是资金部门
     */
    private boolean checkFinance(Integer userId){

        SysUserRole userRole = sysUserRoleMapper.selectRole(userId);
        if(userRole != null){
            return Const.FINANCE_ROLE_ID.equals(userRole.getRoleId());
        }
        return false;
    }

    /**
     * @Date 2018/11/5 13:28
     * @Param [userId]
     * @return boolean
     * @Description // 判断是否是跟单员
     */
     private boolean checkMerch(Integer userId){

        SysUser user = userMapper.selectByPrimaryKey(userId);
        if(null != user) {
            Integer deptId = user.getDeptId();
            return Const.MERCH_DEPT_ID.equals(deptId);
        }
        return false;
    }


    /**
     * @Date 2018/11/5 13:38
     * @Param [userId]
     * @return int
     * @Description // 得到用户角色 2表示跟单 3表示资金
     */
    private int getUserRole(Integer userId){

        if(checkMerch(userId)) {
            return Const.MERCH_ROLE;
        }
        if(checkFinance(userId)){
            return Const.FINANCE_ROLE;
        }

        return -1;
    }

    /**
     * @Date 2018/11/5 14:51
     * @Param [userId, orderNo, opinion]
     * @return void
     * @Description // 跟单员同意之后的推送
     */
    private void messagePush(Integer userId, Long orderNo, String opinion){

        //极光推送
        String title = "【华鼎云商城】订单审核通过";
        String body = "您好，您的采购订单" + orderNo + "已经通过商城审核，请您尽快在交易时间内完成付款操作" + "备注信息：" + (opinion == null ? "" : opinion);
        String userName = userMapper.selectByPrimaryKey(userId).getUsername();
        String type = "order_status";
        String value = orderNo.toString();
        try {
            jpushController.jPushMethod(title, body, userName, type, value);
        } catch (IOException e) {
            System.out.println("消息推送失败");
        }

        //PC推送
        Map<String, String> pcMap = new HashMap<>(16);
        pcMap.put("userName", userName);
        pcMap.put("type", type);
        pcMap.put("body", body);
        pcMap.put("title", title);
        pcMap.put("value", value);
        pcMap.put("isImport", 1 + "");
        String url = envUtil.getPreUrl("order");

        String result = HttpClientUtil.doGetWithParam(url + "/msg/directPcMsgPush", pcMap);
        log.info(result);

        Map<String, String> map = new HashMap<>(16);
        //短信推送
        map.clear();
        map.put("mobile", userName);
        map.put("orderNo", orderNo.toString());
        map.put("text", title);
        String messageResult = HttpClientUtil.doGetWithParam("http://47.98.167.99:9999/admin/orderMsg", map);
        log.info(messageResult);
    }

   /**
    * @Date 2018/11/8 11:19
    * @Param [customerId, price]
    * @return void
    * @Description // 更新欠款和账期信息
    */
   void updateUnPaymentAndStatement(Integer customerId, BigDecimal price) throws InterruptedException {

            Calendar calendar = Calendar.getInstance();
            Integer year = calendar.get(Calendar.YEAR);
            Integer month = calendar.get(Calendar.MONTH) + 1;
            SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectAccount(customerId, year, month);
            if (null == sysCustomerStatement) {
                List<SysCustomer> sysCustomerList = new ArrayList<>();
                sysCustomerList.add(customerMapper.selectByPrimaryKey(customerId));
                taskService.updatePaymentStatement(sysCustomerList);
            }

            try {
                sysCustomerStatementMapper.updatePayment(year, month, price, customerId);
            } catch (Exception e) {
                e.printStackTrace();
            }

            MallPaymentUnpay mallPaymentUnpay = mallPaymentUnpayMapper.selectByPrimaryKey(customerId);
            if (null == mallPaymentUnpay) {
                mallPaymentUnpay = new MallPaymentUnpay();
                mallPaymentUnpay.setCustomerId(customerId);
                mallPaymentUnpay.setUnpayPayment(price);
                mallPaymentUnpayMapper.insert(mallPaymentUnpay);
            } else {
                price = mallPaymentUnpay.getUnpayPayment().add(price);
                mallPaymentUnpay.setUnpayPayment(price);
                mallPaymentUnpayMapper.updateByPrimaryKey(mallPaymentUnpay);
            }
    }
}
